var express = require('express');
var router = express.Router();
var db = require("../util/DataBase.js");

var searchService = require("../service/search.js");

router.get("/recommend", function (req, res, next) {

  if(!(req.query.hasOwnProperty("kw") && req.query.kw.length > 0)) {
    res.json({
      kw: req.query.kw,
      results: []
    });
    return;
  }

  searchService.recommend(req.query.kw.trim(), req.query.top, function (results) {

    res.json({
      kw: req.query.kw,
      results: results
    });

  }, function () {

    res.json({
      kw: req.query.kw,
      results: []
    });

  });

});

module.exports = router;
